'use strict'


//子节点管理器
class ChildsManger {
  constructor() {
    this._childs = {}
  }

  //根据节点的名称获取节点实例
  getChild(childname) {
    for (let c in this._childs) {
      if (this._childs[c].getName() === childname) {
        return this._childs[c]
      }
    }
  }

  //添加一个child节点
  addChild(child) {
    let key = child.getName()
    if (this._childs[key]) {
      console.log("child node " + key + " exists")
      return
    }
    this._childs[key] = child
  }

  //删除一个child
  dropChild(child) {
    let key = child.getName()
    delete this._childs[key]
  }

  //删除一个child
  dropChildByID(childId) {
    delete this._childs[childId]
  }

  //调用子节点的接口
  callChild(childname, msgName, msg, cb) {
    let child = this.getChild(childname)
    child.callChild(msgName, msg, cb)
  }

}

module.exports = ChildsManger
